#include "ObMountain.h"
#include "D3DApp.h"
#include "Collision.h"

IDirect3DTexture9* ObMountain::mTex = 0;

ObMountain::ObMountain(){
	setId(Mountain_Small);
	mInfo	= 0;
}

ObMountain::ObMountain(MOUNTAIN nid){
	setId(nid);
	mInfo	= 0;
}

ObMountain::~ObMountain(){

}

bool ObMountain::setId(int nid){
	if(checkType(nid)){
		if(nid == Mountain_Big){
			mID = Mountain_Big;
			mWidth	= 112;
			mHeight = 48;
			mSpritePos.x = 60;
			mSpritePos.y = 30;

		}else{
			mID = Mountain_Small;
			mWidth	= 80;
			mHeight = 32;
			mSpritePos.x = 60;
			mSpritePos.y = 80;
		}
		mCenter = D3DXVECTOR3(mWidth/2,mHeight/2,0.0f);
		return true;
	}else{
		return false;
	}
}

bool ObMountain::checkType(int obID){
	if(obID<Mountain_Big || obID>Mountain_Small){
		return false;
	}else{
		return true;
	}
}

void ObMountain::update(float dt){

}

void ObMountain::render(ID3DXSprite* mSprite){
	if(mID == 0) return;
	if(mTex == 0) return;

	HR(gDevice->SetRenderState(D3DRS_ALPHATESTENABLE,true));
	D3DXMATRIX S;
	D3DXMatrixScaling(&S,1.0f,-1.0f,1.0f);
	D3DXMATRIX R;
	D3DXMatrixRotationZ(&R, mRotation);
	D3DXMATRIX T;
	D3DXMatrixTranslation(&T,mPos.x - gCamera->getPos().x, mPos.y - gCamera->getPos().y, 0);
	mSprite->SetTransform(&(S*R*T));
	RECT rect = calRenderRECT(mSpritePos,mWidth,mHeight);
	mSprite->Draw(mTex,&rect,&mCenter,0,D3DCOLOR_XRGB(255, 255, 255));
	mSprite->Flush();
	gDevice->SetRenderState(D3DRS_ALPHATESTENABLE,false);

}

bool ObMountain::loadTexture(){
	if(mTex == 0){
		D3DXCreateTextureFromFileEx(gDevice,"Sprites/Mountain.png",
			D3DX_DEFAULT_NONPOW2,
			D3DX_DEFAULT_NONPOW2,
			0,
			0,
			D3DFMT_UNKNOWN,
			D3DPOOL_MANAGED,
			D3DX_DEFAULT,
			D3DX_DEFAULT,
			D3DCOLOR_XRGB(0,0,0),
			NULL,
			NULL,
			&mTex);
		return true;
	}else{
		return false;
	}
}

bool ObMountain::releaseTexture(){
	if(mTex != 0){
		ReleaseCOM(mTex);
		return true;
	}else{
		return false;
	}
}

